package com.vm.activity;

import java.io.File;
import java.util.List;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.vm.commons.CurrentSetting;
import com.vm.commons.StringUtil;
import com.vm.commons.VMConst;
import com.vm.db.DownInfoDao;
import com.vm.model.ActiveSoft;
import com.vm.model.DownInfo;
import com.vm.myview.ViewDownUtils;
import com.vm.utils.MyDisplayImageOptions;
import com.wwtscapp.apk.R;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 
 * @ProjectName: [WeiMai]
 * @Author: [kent.wang]
 * @CreateDate: [2015年2月4日 下午3:55:58]
 * @Version: [v1.0]
 * 
 */
public class DownloadedAcitivity extends Activity {

	ListView lv_downloading;
	DownInfoDao infoDao;
	ViewDownUtils viewDownUtils;
	ActiveSoft activeSoft;
	LinearLayout found_ll_1;
	Button btn_download;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.download_complete);

		init();
		infoDao = new DownInfoDao(this);
		List<DownInfo> list = infoDao.getDownloaded();

		int i = 0;
		for (DownInfo downInfo : list) {
			activeSoft = new ActiveSoft();
			activeSoft.setSoftId(downInfo.getSoftId());
			activeSoft.setName(downInfo.getName());
			activeSoft.setImageUrl(downInfo.getImageUrl());
			found_ll_1.addView(dyAddView(i, activeSoft, 1));
			i++;
		}

	}

	public void init() {
		found_ll_1 = (LinearLayout) findViewById(R.id.found_ll_1);
	}

	/**
	 * 动态创建LinearLayout
	 * 
	 * @param i
	 * @param activeSoft
	 * @param progress
	 *            1.初始化进度条,0不初始化进度条
	 * @return
	 */
	public LinearLayout dyAddView(int i, final ActiveSoft activeSoft,
			int progress) {

		LinearLayout linearLayout = (LinearLayout) getLayoutInflater().inflate(
				R.layout.found_item, null);

//		RelativeLayout rlayout_info = (RelativeLayout) linearLayout
//				.findViewById(R.id.rlayout_info);

		TextView tv_softName = (TextView) linearLayout
				.findViewById(R.id.tv_softName);
		ImageView iv_softImage = (ImageView) linearLayout
				.findViewById(R.id.iv_softImage);
		TextView tv_softSize = (TextView) linearLayout
				.findViewById(R.id.tv_softSize);
		RatingBar rb_star = (RatingBar) linearLayout.findViewById(R.id.rb_star);

		btn_download = (Button) linearLayout.findViewById(R.id.btn_download);
		btn_download.setTag(activeSoft.getName());
		btn_download.setText("安装");
		btn_download.setOnClickListener(downClickListener);

		if (activeSoft.getName() != null) {
			// String softName =
			// StringUtil.getShotName(soft.getName());
			tv_softName.setText(activeSoft.getName());
		}
		// 大小
		if (activeSoft.getSize() != null) {
			float size = activeSoft.getSize();
			tv_softSize.setText(Float.toString(size) + "M");
		}
		// 星级
		if (activeSoft.getStar() != null) {
			float fstar = activeSoft.getStar();
			rb_star.setRating(fstar / 2);
		}

		ImageLoader.getInstance().displayImage(
				VMConst.URL + activeSoft.getImageUrl(), iv_softImage,
				MyDisplayImageOptions.getInstance());

//		OnClickListener ll_listener = new OnClickListener() {
//
//			@Override
//			public void onClick(View v) {
//				Intent intent = new Intent(DownloadedAcitivity.this,
//						DetailActivity.class);
//
//				intent.putExtra("softId", activeSoft.getSoftId());
//				DownloadedAcitivity.this.startActivityForResult(intent, 10);
//			}
//		};

		/**
		 * 加载一些列下载有关的东西
		 */

		// downloadService.initProgressBar(soft);
		return linearLayout;
	}

	/**
	 * 下载按钮监听
	 */
	public OnClickListener downClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {

			// 执行安装
			Toast toast3 = Toast
					.makeText(DownloadedAcitivity.this, "正在安装", 500);
			toast3.setGravity(Gravity.CENTER, 0, 0);
			toast3.show();

			String tag = v.getTag().toString();
			File apkfile = new File(StringUtil.getSdApkUrl(tag));
			if (!apkfile.exists()) {
				return;
			}
			if (CurrentSetting.autoinstall == 1) {
				Intent i = new Intent(Intent.ACTION_VIEW);
				i.setDataAndType(Uri.parse("file://" + apkfile.toString()),
						"application/vnd.android.package-archive");
				v.getContext().startActivity(i);
			}
		}
	};

}
